﻿using System;
using Framework.MVVM.Model;
using Identity.IService;
using Identity.Service;
using Log.IService;
using Log.Service;
using Common.Service;
using Common.IService;
using Account.IService;
using Account.Service;
using Framework.MVC;
using Log.Model;
using Identity.Model;
using Account.Model;
using Account.IService.Entry;
using Identity.IService.Entry;
using Account.Model.Behavior;


namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IUserInfoService), typeof(UserInfoService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class NewUserInfoModel : AsyncModelBase, IWriteLog, IValidateIdentity, INewUserInfo
    {
        #region Field
		
        private const string m_empty_identity = "nuinfo000";
        private const string m_error_identity = "nuinfo001";
        private const string m_complete_identity = "nuinfo002";
        private const string m_error_userinfo = "nuinfo003";
        private const string m_complete_userinfo = "nuinfo004";

        private const string m_resource_user = "u000";
         private const string m_entry="entry";
	    #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateIdentity(m_resource_user, m_empty_identity, m_error_identity, m_complete_identity);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateIdentity

        public string IdentityCode { get; set; }
        public string Source { get; set; }
        public bool IsOnce { get; set; }

        public void OnValidateIdentityComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);
            AccountCode = entry.value;
            this.NewUserInfo(m_error_userinfo, m_complete_userinfo);
        }
           

        public void OnValidateIdentityError(string key)
        {
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_resource_user });
        }

        #endregion IValidateIdentity

        #region INewUserInfo

        public string AccountCode { get; set; }

        public string Birthday { get; set; }

        public string Location { get; set; }

        public string FixedPhone { get; set; }

        public string Mobile { get; set; }

        public void OnNewUserInfoComplete(string key, UserInfoEntry entry)
        {
            OnComplete(m_entry,entry);
            this.Log(key, entry);
        }

        public void OnNewUserInfoError(string key)
        {
            this.Log(key, new UserInfoEntry { code = AccountCode, birthday = Birthday, fixedPhone = FixedPhone, mobile = Mobile });
        }

        #endregion INewUserInfo
    } 
}